/*
 * Copyright© 2013-2018 YZTC 
 * Author zhenghl 
 * 本公司保留所有下述内容的权利; 
 * 本内容为保密信息，仅限本公司内部使用; 
 * 非经本公司书面许可，任何人不得外泄或用于其他目的; 
*/
package com.wxmp.wxapi.ctrl; 

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wxmp.core.common.BaseCtrl;
import com.wxmp.core.util.AjaxResult;
import com.wxmp.core.util.StringUtil; 
import com.wxmp.core.util.WebUtil;
import com.wxmp.wxapi.process.MpAccount; 
import com.wxmp.wxapi.process.WxMemoryCacheClient; 
import com.wxmp.wxcms.domain.RedGroupPack;
import com.wxmp.wxcms.domain.RedPack;
import com.wxmp.wxcms.service.RedpackService; 
/**
 * 微信支付与开发者服务器交互接口
 */
@Controller
@RequestMapping("/wxapi")
public class WxPayApiCtrl extends BaseCtrl{
	private static Logger log = LogManager.getLogger(WxPayApiCtrl.class);
    @Resource
    private RedpackService redpackService;
	/**
	 * POST 请求：进行消息处理；
	 * @throws Exception 
	 * */
	@RequestMapping(value = "/pay/redpack/tmp", method = RequestMethod.POST)
	public @ResponseBody  AjaxResult doPostRedpackTmp(HttpServletRequest request,HttpServletResponse response) throws Exception {
	    String ticket = request.getParameter("ticket");
	    if(!"zhenghl_123".equals(ticket))
	        return AjaxResult.failure("发送红包失败，凭证不正确");
        String account = request.getParameter("account");
        if(StringUtil.isEmpty(account))
            return AjaxResult.failure("发送红包失败，账号不正确");
	    //获取缓存中的唯一账号
        MpAccount mpAccount = WxMemoryCacheClient.getMpAccount(account);
        //活动信息
        String openids = StringUtil.isEmpty(request.getParameter("openids"))?"":request.getParameter("openids");
        //红包祝福语
        String wishing = StringUtil.isEmpty(request.getParameter("wishing"))?"感谢您参加女神节活动，祝您3.8女神节快乐！":request.getParameter("wishing");
        //活动名称
        String act_name = StringUtil.isEmpty(request.getParameter("act_name"))?"女神节抢红包活动":request.getParameter("act_name");
        //备注
        String remark = StringUtil.isEmpty(request.getParameter("remark"))?"拆的越多，抢得越多，加油吧！！":request.getParameter("remark");
        //活动信息
        String risk_info = StringUtil.isEmpty(request.getParameter("risk_info"))?"":request.getParameter("risk_info");
        //场景id
        String scene_id = StringUtil.isEmpty(request.getParameter("scene_id"))?"":request.getParameter("scene_id");
        String certFile = request.getServletContext().getRealPath("/wxkeystore/"+mpAccount.getMchId()+".p12");
        String[] ids = openids.split(",");
        Map<String,Boolean> map = new HashMap<String,Boolean>();
        for(String openid:ids) {
            RedPack bean = new RedPack();
            bean.setAct_name(act_name);
            bean.setClient_ip(WebUtil.getRemoteAddr(request));
            bean.setNonce_str(StringUtil.getRandomLengthString(32));
            bean.setRe_openid(openid);
            bean.setRemark(remark);
            //bean.setRisk_info(risk_info);//选填
            //bean.setScene_id(scene_id);//选填
            bean.setTotal_amount(100+(int)Math.ceil(Math.random() * 200));
            bean.setTotal_num(1);
            bean.setWishing(wishing);
            boolean result = redpackService.postRedpack(certFile, bean, mpAccount);
            map.put(openid,result);
        }
        return AjaxResult.success(map, "发送红包成功");
	 
	} 
	@RequestMapping(value = "/pay/redpack", method = RequestMethod.POST)
    public @ResponseBody  AjaxResult doPostRedpack(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取缓存中的唯一账号
        MpAccount mpAccount = WxMemoryCacheClient.getMpAccount();
        //活动信息
        String openids = StringUtil.isEmpty(request.getParameter("openids"))?"":request.getParameter("openids");
        //红包祝福语
        String wishing = StringUtil.isEmpty(request.getParameter("wishing"))?"感谢您参加女神节活动，祝您3.8女神节快乐！":request.getParameter("wishing");
        //活动名称
        String act_name = StringUtil.isEmpty(request.getParameter("act_name"))?"女神节抢红包活动":request.getParameter("act_name");
        //备注
        String remark = StringUtil.isEmpty(request.getParameter("remark"))?"拆的越多，抢得越多，加油吧！！":request.getParameter("remark");
        //活动信息
        String risk_info = StringUtil.isEmpty(request.getParameter("risk_info"))?"":request.getParameter("risk_info");
        //场景id
        String scene_id = StringUtil.isEmpty(request.getParameter("scene_id"))?"":request.getParameter("scene_id");
        String certFile = request.getServletContext().getRealPath("/wxkeystore/"+mpAccount.getMchId()+".p12");
        String[] ids = openids.split(",");
        Map<String,Boolean> map = new HashMap<String,Boolean>();
        for(String openid:ids) {
            RedPack bean = new RedPack();
            bean.setAct_name(act_name);
            bean.setClient_ip(WebUtil.getRemoteAddr(request));
            bean.setNonce_str(StringUtil.getRandomLengthString(32));
            bean.setRe_openid(openid);
            bean.setRemark(remark);
            //bean.setRisk_info(risk_info);//选填
            //bean.setScene_id(scene_id);//选填
            bean.setTotal_amount(100+(int)Math.ceil(Math.random() * 10));
            bean.setTotal_num(1);
            bean.setWishing(wishing);
            boolean result = redpackService.postRedpack(certFile, bean, mpAccount);
            map.put(openid,result);
        }
        return AjaxResult.success(map, "发送红包成功");
     
    } 
	/**
     * POST 请求：进行消息处理；
	 * @throws Exception 
     * */
    @RequestMapping(value = "/pay/redgrouppack", method = RequestMethod.POST)
    public @ResponseBody AjaxResult doPostGroupRedpack(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取缓存中的唯一账号
        MpAccount mpAccount = WxMemoryCacheClient.getMpAccount();
        //活动信息
        String openids = StringUtil.isEmpty(request.getParameter("openids"))?"":request.getParameter("openids");
        //红包祝福语
        String wishing = StringUtil.isEmpty(request.getParameter("wishing"))?"感谢您参加女神节活动，祝您3.8女神节快乐！":request.getParameter("wishing");
        //活动名称
        String act_name = StringUtil.isEmpty(request.getParameter("act_name"))?"女神节抢红包活动":request.getParameter("act_name");
        //备注
        String remark = StringUtil.isEmpty(request.getParameter("remark"))?"拆的越多，抢得越多，加油吧！！":request.getParameter("remark");
        //活动信息
        String risk_info = StringUtil.isEmpty(request.getParameter("risk_info"))?"":request.getParameter("risk_info");
        //场景id
        String scene_id = StringUtil.isEmpty(request.getParameter("scene_id"))?"":request.getParameter("scene_id");
        //红包金额设置方式

        String certFile = request.getServletContext().getRealPath("/wxkeystore/"+mpAccount.getMchId()+".p12");
        
        String[] ids = openids.split(",");
        Map<String,Boolean> map = new HashMap<String,Boolean>();
        for(String openid:ids) {
            RedGroupPack bean = new RedGroupPack();
            bean.setAct_name(act_name);
            bean.setAmt_type("ALL_RAND");
            bean.setClient_ip(WebUtil.getRemoteAddr(request));
            bean.setNonce_str(StringUtil.getRandomLengthString(32));
            bean.setRe_openid(openid);
            bean.setRemark(remark);
            //bean.setRisk_info(risk_info);//选填
            //bean.setScene_id(scene_id);//选填
            bean.setTotal_amount(300);
            bean.setTotal_num(3);
            bean.setWishing(wishing);
            boolean result = redpackService.postedGroupPack(certFile, bean, mpAccount);
            map.put(openid,result);
        }
        return AjaxResult.success(map, "发送裂变红包成功");
         
    } 
    
}
